home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / Roaster-Java-WA-HTTP-CGI hack / wa / http / TinyHTTPd.java < prev    next >
Encoding:
Java Source  |  1996-06-22  |  1.7 KB  |  85 lines  |  [TEXT/Rstr]

  1. import java.net.*;
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. /*
  6. class WACGI
  7. {
  8.     static native void SendRequest(String request);
  9.     static native String GetResponse();
  10.     static native void Done();
  11. }
  12. */
  13. public class TinyHTTPd
  14. {
  15.     public static void main( String argv[] ) throws IOException
  16.     {
  17.         int port = 80;
  18.         
  19.         //System.out.println( "HTTPd starting..." );
  20.  
  21.         if (argv.length >= 1)
  22.             port = Integer.parseInt(argv[0]);
  23.         System.out.println( "HTTPd starting on port " + port + "...");
  24.         ServerSocket ss = new ServerSocket(port);
  25.  
  26.         while (true)
  27.         {
  28.             Socket sock = ss.accept();
  29.             
  30.             try
  31.             {
  32.                 OutputStream out = sock.getOutputStream();
  33.                 String req = new DataInputStream(sock.getInputStream()).readLine();
  34.                 System.out.println( "Request:" + req );
  35.                 
  36.                 StringTokenizer st = new StringTokenizer(req);
  37.                 String firstToken = st.nextToken();
  38.                 
  39.                 boolean another = true;
  40.                 
  41.                 if ((st.countTokens() >= 2) &&
  42.                      firstToken.equals("GET"))
  43.                 {
  44.                     if ( (req = st.nextToken()).startsWith("/"))
  45.                         req = req.substring(1);
  46.                         
  47.                     if (req.equals("QUIT"))
  48.                         another = false;
  49.                     else
  50.                     {
  51.                         req = "/Ricasso/cgi.txt";
  52.                     }
  53.                     try
  54.                     {
  55.                         FileInputStream fis = new FileInputStream(req);
  56.                         int av = fis.available();
  57.                         byte [] data = new byte [av];
  58.                         fis.read(data);
  59.                         out.write(data);
  60.                         out.flush();
  61.                         //out.close();
  62.                     }
  63.                     catch (FileNotFoundException e)
  64.                         new PrintStream(out).println("404 Not Found");
  65.                 }
  66.                 else
  67.                     new PrintStream(out).println("400 Bad Request");
  68.                 
  69.                 sock.close();
  70.                 if (!another)
  71.                 {
  72.                     System.out.println("Quitting...");
  73.                     //WACGI.Done();
  74.                     break;
  75.                 }
  76.             }
  77.             catch (IOException e)
  78.                 System.out.println("I/O Error " + e);
  79.  
  80.         }
  81.         
  82.     }
  83. }
  84.  
  85.